class ListNode {
  constructor(val, next = null) {
    this.val = val; // 节点值
    this.next = next; // 指向下一个节点的指针
  }
}
const head = new ListNode(1, new ListNode(2, new ListNode(3)));
console.log(head);

function reverse(head) {
  let cur = head;
  let prev = null;
  while (cur) {
    let temp = cur.next;
    cur.next = prev;
    prev = cur;
    cur = temp;
  }
  return prev;
}
console.log(reverse(head));
